unit IsisEngine;

interface

uses
   SysUtils,StdCtrls,ComCtrls,ExtCtrls,Controls,Math,Forms;

type

   TLevel=(lvEasy,lvMedium,lvHard);

   TIsis=class
   private
      fHits:Integer;
      fErrors:Integer;
      fLevel:TLevel;
      fPanel:TPanel;
      fDisplay:TLabel;
      fTimerCreate:TTimer;
      FTimerDestroy:TTimer;
      
      procedure SetHitsandErrors(Hit:Boolean);
      procedure ChangeLevel(Level:TLevel);
      procedure ShowLabelOnPanel(Top,Left:Integer);
      procedure RemoveLabelfromPanel;
      procedure CustomOnClickLabel(Sender:TObject);
      procedure CustomOnClickPanel(Sender:TObject);
      procedure TimerDestroyOnTimer(Sender:TObject);
      procedure TimerCreateOnTimer(Sender:TObject);
   public
      constructor IsisStart(Surface:TPanel;Display:TLabel);reintroduce;

      procedure Start;
      procedure ReStart;
      procedure Stop;

      property Level:TLevel read fLevel write ChangeLevel;
   end;

var
   lbl:TLabel;

implementation

{ TIsis }

procedure TIsis.ChangeLevel(Level: TLevel);
var
   iLevel:Integer;
begin
   Stop;

   iLevel:=Integer(Level)+1;

   fTimerCreate.Interval:=1000 div iLevel;
   FTimerDestroy.Interval:=1000 div iLevel;

   ReStart;
end;

procedure TIsis.CustomOnClickLabel(Sender: TObject);
begin
   SetHitsandErrors(True);
end;

procedure TIsis.CustomOnClickPanel(Sender: TObject);
begin
   SetHitsandErrors(False);
end;

constructor TIsis.IsisStart(Surface: TPanel; Display: TLabel);
var
   iLevel:Integer;
begin
   fLevel:=lvEasy;
   iLevel:=1;
   fHits:=0;
   fErrors:=0;

   fDisplay:=Display;
   fPanel:=Surface;

   fPanel.OnClick:=CustomOnClickPanel;
   
   fTimerCreate:=TTimer.Create(nil);
   FTimerDestroy:=TTimer.Create(nil);

   fTimerCreate.Interval:=1000 div iLevel;
   FTimerDestroy.Interval:=1000 div iLevel;

   fTimerCreate.Enabled:=False;
   FTimerDestroy.Enabled:=False;

   fTimerCreate.OnTimer:=TimerCreateOnTimer;
   FTimerDestroy.OnTimer:=TimerDestroyOnTimer;
end;

procedure TIsis.RemoveLabelfromPanel;
begin
   if(lbl<>nil)then
      lbl.Destroy;
end;

procedure TIsis.ReStart;
begin
   fErrors:=0;
   fHits:=0;

   fTimerCreate.Enabled:=True;
   FTimerDestroy.Enabled:=False;
end;

procedure TIsis.SetHitsandErrors(Hit: Boolean);
begin
   if(Hit)then
      Inc(fHits)
   else
      Inc(fErrors);

   fDisplay.Caption:='Hits: '+IntToStr(fHits)+' Errors: '+IntToStr(fErrors);
   Application.ProcessMessages;
end;

procedure TIsis.ShowLabelOnPanel(Top, Left: Integer);
begin
   lbl:=TLabel.Create(nil);
   lbl.Parent:=fPanel;
   lbl.Color:=$FF0000;
   lbl.SetBounds(Left,Top,20,20);
   lbl.OnClick:=CustomOnClickLabel;
end;

procedure TIsis.Start;
begin
   fTimerCreate.Enabled:=True;
   FTimerDestroy.Enabled:=False;
end;

procedure TIsis.Stop;
begin
   fTimerCreate.Enabled:=False;
   FTimerDestroy.Enabled:=False;
end;

procedure TIsis.TimerCreateOnTimer(Sender: TObject);
var
   iLeft,
   iTop:Integer;
begin
   Randomize;

   iLeft:=RandomRange(0,fPanel.Width);
   iTop:=RandomRange(0,fPanel.Height);
   ShowLabelOnPanel(iTop,iLeft);

   fTimerCreate.Enabled:=False;
   FTimerDestroy.Enabled:=True;
end;

procedure TIsis.TimerDestroyOnTimer(Sender: TObject);
begin
   RemoveLabelfromPanel;
   FTimerDestroy.Enabled:=False;
   fTimerCreate.Enabled:=True;
end;

end.
